package org.geek.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.geek.core.bean.ExtCheckTree;
import org.geek.core.bean.ExtTree;

/**
 * 树的工具类
 */
public class TreeUtil {

	/**
	 * 将menus组成一颗树
	 * @param menus 菜单集合
	 * @param parentId 父级菜单Id
	 * @return
	 */
	public static List<ExtTree> getTree(List<ExtTree> menus,int parentId){
		ExtTree tree = new ExtTree();
		tree.setId(parentId);
		return getNode(menus,tree).getChildren();
	}
	
	/**
	 * 组将一颗带checkbox的树
	 * @param menus 不带权限的菜单集合
	 * @param parentId 父级菜单Id
	 * @param menuHasAuth 带权限菜单集合
	 * @return
	 */
	public static List<ExtCheckTree> getTreeHasCheckBox(List<ExtTree> menus, int parentId,
			List<ExtTree> menuHasAuth) {
		ExtTree menu = new ExtTree();
		menu.setId(parentId);
		Map<Integer,ExtTree> map = new HashMap<Integer,ExtTree>();
		for(ExtTree t : menuHasAuth){
			map.put(t.getId(), t);
		}
		return getCheckNode(menus, menu,map).getChildren();
	}
	
	/**组桌面模式的开始菜单
	 * @param menus
	 * @param parentId
	 * @return
	 */
	public static List<ExtTree> getDesktopTree(List<ExtTree> menus,int parentId){
		List<ExtTree> list = new ArrayList<ExtTree>();
		return getDesktopNode(parentId,menus, "",list,"");
	}
	
	private static ExtTree getNode(List<ExtTree> list, ExtTree node) {
		ExtTree tree = new ExtTree();
		tree.setId(node.getId());
		tree.setText(node.getName());
		tree.setName(node.getName());
		tree.setIconCls(node.getIconCls());
		tree.setChildren(new ArrayList<ExtTree>());
		if (list == null) {
			return tree;
		}
		List<ExtTree> childList = getChildList(list, node);
		if (childList.size()>0) {
			List<ExtTree> lt = new ArrayList<ExtTree>();
			tree.setUrl("");
			tree.setLeaf(false);
			Iterator<ExtTree> it = childList.iterator();
			while (it.hasNext()) {
				ExtTree Menus = (ExtTree) it.next();
				list.remove(Menus);
				lt.add(getNode(list, Menus));
			}
			tree.setChildren(lt);
		} else {
			tree.setUrl(node.getUrl());
			tree.setLeaf(true);
		}
		return tree;
	}

	/**
	 * 得到子节点列表
	 */
	private static List<ExtTree> getChildList(List<ExtTree> list, ExtTree Menus) {
		List<ExtTree> li = new ArrayList<ExtTree>();
		Iterator<ExtTree> it = list.iterator();
		while (it.hasNext()) {
			ExtTree temp = (ExtTree) it.next();
			if (temp.getParentId() == Menus.getId()) {
				li.add(temp);
			}
		}
		return li;
	}
	
	private static ExtCheckTree getCheckNode(List<ExtTree> list, ExtTree node,
			Map<Integer, ExtTree> map) {
		ExtCheckTree tree = new ExtCheckTree();
		tree.setId(node.getId());
		tree.setText(node.getText());
		tree.setName(node.getName());
		tree.setIconCls(node.getIconCls());
		tree.setChildren(new ArrayList<ExtCheckTree>());
		if(map.get(node.getId())!=null){
			tree.setChecked(true);
			map.remove(node.getId());
		}
		if (list == null) {
			return tree;
		}
		List<ExtTree> childList = getChildList(list, node);
		if (childList.size()>0) {
			List<ExtCheckTree> lt = new ArrayList<ExtCheckTree>();
			tree.setUrl("");
			tree.setLeaf(false);
			Iterator<ExtTree> it = childList.iterator();
			while (it.hasNext()) {
				ExtTree trees = (ExtTree) it.next();
				list.remove(trees);
				lt.add(getCheckNode(list, trees,map));
			}
			tree.setChildren(lt);
		} else {
			tree.setUrl(node.getUrl());
			tree.setLeaf(true);
		}
		return tree;
	}
	
	private static List<ExtTree> getDesktopNode(int parentId,
			List<ExtTree> allHasRightMenu, String parentPath, List<ExtTree> list,String patentName) {
		List<ExtTree> ls = new ArrayList<ExtTree>();
		ls.addAll(allHasRightMenu);
		for (ExtTree menu : allHasRightMenu) {
			if (menu.getParentId() == parentId) {
				ExtTree tree = new ExtTree();
				tree.setId(menu.getId());
				tree.setText(menu.getText());
				tree.setName(menu.getName());
				tree.setIconCls(menu.getIconCls());
				tree.setParentPath(parentPath);
				tree.setUrl(menu.getUrl());
				tree.setPatentName(patentName);
				ls.remove(menu);
				List<ExtTree> listChildren = getDesktopNode(menu.getId(), ls,
						tree.getParentPath() + menu.getName(), list,menu.getName());
				tree.setChildren(listChildren);
				if (!"".equals(menu.getUrl())) {//只留叶子节点
					list.add(tree);
				}
			}
		}
		return list;
	}
}
